package com.example.springbootfoodsafety.contrlloer;

import com.example.springbootfoodsafety.po.Article;
import com.example.springbootfoodsafety.serviceimpl.ArtcleServiceImpl;
import com.example.springbootfoodsafety.util.CodeMsg;
import com.example.springbootfoodsafety.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ArtcleContrlloer {
    @Autowired
    ArtcleServiceImpl service;

    /***
     * 获取类别，浏览+1
     * @param id
     * @return
     */
    @GetMapping("ArtcleContrlloer.getTagName")
    public Result getTagName(Integer id) {
        System.out.println(id);
        if(service.getTagName(id)!=null)
        {
            addView(id);
            return Result.success(service.getTagName(id));
        }
        return Result.error(new CodeMsg(404,"找不到"));
    }

    /**
     * 修改浏览量功能
     * @param id
     * @return
     */
    public boolean addView(Integer id){
        System.out.println("增加浏览");
        int count=service.getView(id);
      return  service.modifyView(id,++count);
    }

    /***
     * 点赞功能
     * @param id
     * @return
     */
    @PostMapping("/artcle.ModifyZan")
    public boolean modifyZan( Integer id) {
        return service.modifyZan(id);
    }
    @PostMapping("/artcle.getZan")
    public Integer getZan(Integer id) {
        return service.getZan(id);
    }

    @PostMapping("/artcle.getTagForId")
    public Result getTagForId( Integer id){
        System.out.println(id);
        List<Article> list= service.getTagForId(id);
        if(list!=null)
        {
            return Result.success(list);
        }
        return Result.error(new CodeMsg(400,"该类别下还没有文章"));
    }


}
